package com.example.my.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.my.entity.Permission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {

    @Select("SELECT p.* " +
            "FROM permissions p " +
            "JOIN role_permission rp ON p.id = rp.perm_id " +
            "JOIN user_role ur ON ur.role_id = rp.role_id " +
            "WHERE ur.user_id = #{userId} AND p.deleted = 0")
    List<Permission> getPermissionsByUserId(@Param("userId") Long userId);

    @Select("SELECT p.* " +
            "FROM permissions p " +
            "INNER JOIN role_permission rp ON p.id = rp.perm_id " +
            "WHERE rp.role_id = #{roleId} AND p.deleted = 0")
    List<Permission> getPermissionsByRoleId(@Param("roleId") Long roleId);
}